<template>
  <div>
    <el-form ref="myform" v-loading="loading" :model="formItem" label-width="100px" size="mini" class="form-view">
      <el-row>
        <el-col :span="12">
          <el-form-item label="标题" prop="title">
            {{ formItem.title }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接收日期" prop="receiveDate">
            {{ formItem.receiveDate }}
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="资产明细">
            <el-table
              :loading="false"
              :data="formItem.assetList"
              border
              class="fixed-table-table"
              size="mini"
              max-height="240px"
            >
              <el-table-column
                label="序号"
                type="index"
                width="50"
                header-align="center"
                align="center"
              />
              <el-table-column v-for="column in insideColumns" :key="column.key" header-align="center" align="center" :label="column.title" :prop="column.key" :sortable="column.sortable">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
              <el-table-column label="操作" header-align="center" align="center" width="100" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                  <el-button type="text" size="mini" icon="el-icon-edit" title="编辑" @click="edit(row)" />
                  <el-button type="text" size="mini" icon="el-icon-delete" title="删除" @click="remove(row)" />
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="">
            <el-button size="mini" @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="commit">提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 资产的弹窗 -->
    <FormDialog title="编辑资产" :append-to-body="true" :show="assetDialogFormVisible" @on-ok="onSave" @on-cancel="assetDialogFormVisible = false">
      <EditAsset v-if="assetDialogFormVisible" ref="editAsset" :install-asset="editedAsset" @change="onEditOK" />
    </FormDialog>
  </div>
</template>
<script>
import { confirmDetail, confirm } from '@neu/dcam-ui/api/rukuFinanceSL'
import { formatDate } from '@neu/biz-core/utils/utils'// 资产表格组件
import EditAsset from './editAsset'
import FormDialog from '@neu/biz-core/components/FormDialog'

const formItem = {
  title: '',
  receiveDate: '',
  assetList: []
}
export default {
  components: {
    EditAsset,
    FormDialog
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      formItem: {
        ...formItem
      },
      insideColumns: [
        { title: '资产名称', width: 140, key: 'name', sortable: 'custom', display: true },
        { title: '资产卡片', width: 100, key: 'card', sortable: 'custom', display: true },
        { title: '资产分类', key: 'type',
          render: (h, params) => {
            let txt = params.row.smallTypeName
            if (params.row.middleTypeName) {
              txt = params.row.middleTypeName + ' / ' + txt
            }
            if (params.row.bigTypeName) {
              txt = params.row.bigTypeName + ' / ' + txt
            }
            return h('span', txt)
          }
        },
        { title: '购置日期', width: 100, key: 'purchaseDate', render: (h, params) => { return h('span', formatDate(params.row.purchaseDate, 'YYYY-MM-DD')) }, display: true },
        { title: '品牌', width: 100, key: 'brand', sortable: 'custom', display: true },
        { title: '规格型号', width: 100, key: 'specifications', sortable: 'custom', display: true },
        { title: '序列号', width: 100, key: 'serialnumber', sortable: 'custom', display: true },
        { title: '计量单位', width: 100, key: 'measureUnit', sortable: 'custom', display: true },
        { title: '数量', width: 100, key: 'amount', sortable: 'custom', display: true },
        { title: '使用期限', width: 100, key: 'usePeriod', display: true },
        { title: '金额', width: 100, key: 'money', sortable: 'custom', display: true },
        { title: '用途', width: 100, key: 'purpose', display: true }
        // { title: '维保日期', key: 'maintenanceStartTime',
        //   render: (h, params) => {
        //     const start = formatDate(params.row.maintenanceStartTime, 'YYYY-MM-DD')
        //     const end = formatDate(params.row.maintenanceEndTime, 'YYYY-MM-DD')
        //     return h('span', start + ' 至 ' + end)
        //   }
        // }
        // { title: '使用部门', width: 150, key: 'managementOrgName', sortable: 'custom', display: true },
        // { title: '使用人', width: 100, key: 'managementUserName', sortable: 'custom', display: true }
      ],
      assetDialogFormVisible: false,
      editedAsset: {}
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 获取借用工单信息
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        confirmDetail(this.workOrderNum, this.taskId).then(response => {
          response.data.assetList = response.data.assetList.sort((a, b) => b.id - a.id)
          this.formItem = {
            ...response.data
          }
          this.formItem.receiveDate = formatDate(this.formItem.receiveDate, 'YYYY-MM-DD HH:mm:ss')
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    edit(row) {
      this.editedAsset = row
      this.assetDialogFormVisible = true
    },
    /** 资产弹窗选择确认 */
    onSave() {
      this.$refs.editAsset.save()
      this.assetDialogFormVisible = false
    },
    onEditOK(data) {
      const assets = this.formItem.assetList.filter(it => it.id !== data.id)
      assets.push(data)
      this.formItem.assetList = assets.sort((a, b) => b.id - a.id)
    },
    commit() {
      this.loading = true
      const data = { ...this.formItem }
      data.workOrderNum = this.workOrderNum
      data.taskId = this.taskId
      confirm(data).then(res => {
        if (res.status === 'success') {
          this.$emit('cancelNewOrderForm', 'refresh')
        }
        this.loading = false
      })
    },
    btnCancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    },
    remove(row) {
      const i = this.formItem.assetList.lastIndexOf(row)
      this.formItem.assetList.splice(i, 1)
    }
  }
}
</script>

<style lang="scss" scoped>
  .fixed-table-table {
    margin: 8px 8px 8px 0;
  }
</style>
